<template>
  <div class="app-container">
    <el-tooltip
      class="item"
      effect="dark"
      :content="$t('productGroup.createGroupTip')"
      placement="top-start"
    >
      <el-button
        class="addBtn"
        type="primary"
        icon="el-icon-plus"
        @click="createGroup"
        >{{ $t("productGroup.createGroup") }}</el-button
      >
    </el-tooltip>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline sp_between"
    >
      <el-form-item :label="$t('productGroup.groupName')">
        <el-input v-model="formInline.name" style="width: 350px" />
      </el-form-item>
      <div>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            plain
            @click="onSubmit"
            >{{ $t("productGroup.search") }}</el-button
          >
        </el-form-item>
      </div>
    </el-form>
    <div class="groupTable">
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark">
        <el-table-column
          show-overflow-tooltip
          prop="groupName"
          :label="$t('productGroupTable.groupName')"
        />
        <el-table-column
          prop="description"
          :label="$t('productGroupTable.description')"
        />
        <el-table-column
          prop="quantity"
          :label="$t('productGroupTable.quantity')"
        >
          <template slot-scope="scope">{{
            scope.row.quantity.length
          }}</template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="sequence"
          :label="$t('productGroupTable.sequence')"
          width="300"
        >
          <template slot-scope="scope">{{ scope.row.sequence.label }}</template>
        </el-table-column>
        <el-table-column prop="status" :label="$t('productGroupTable.status')">
          <template slot-scope="scope">{{ scope.row.status.label }}</template>
        </el-table-column>
        <el-table-column width="240" :label="$t('productGroupTable.operating')">
          <template slot-scope="scope">
            <el-button
              style="color: #fb0000"
              size="mini"
              type="text"
              @click="disableGroup(scope.row)"
              >{{
                scope.row.status.label !== "Disable"
                  ? $t("productGroupTable.disable")
                  : $t("productGroupTable.enable")
              }}</el-button
            >
            <el-button size="mini" type="text" @click="editGroup(scope.row)">{{
              $t("productGroupTable.edit")
            }}</el-button>

            <el-tooltip
              class="item"
              effect="dark"
              :content="$t('productGroupTable.managementTip')"
              placement="top-start"
            >
              <el-button
                size="mini"
                type="text"
                @click="toPage(scope.row.id)"
                >{{ $t("productGroupTable.management") }}</el-button
              >
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div class="block cz_center">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="formInline.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <span class="pageText"> {{ $t("pagination.page") }} </span>
      </div>
    </div>
    <!-- 添加产品组 -->
    <el-dialog
      width="700px"
      :title="titleForm"
      :visible.sync="dialogFormVisible"
      @close="resetForm('rulesForm')"
    >
      <el-form
        ref="rulesForm"
        :rules="rulesForm"
        class="createGroup"
        :model="form"
        label-width="160px"
      >
        <el-form-item
          prop="groupName"
          :label="$t('productGroupCreate.name')"
          required
        >
          <el-input
            v-model="form.groupName"
            :placeholder="$t('productGroupCreate.nameTip')"
          />
        </el-form-item>
        <el-form-item :label="$t('productGroupCreate.Description')">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item
          prop="sequenceValue"
          :label="$t('productGroupCreate.sequence')"
          required
        >
          <el-select
            v-model="form.sequenceValue"
            :placeholder="$t('productGroupCreate.sequenceTip')"
            @change="changeSequenceValue"
          >
            <el-option
              v-for="item in storage.get('sequence')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addProductGroup('rulesForm')">{{
          $t("publicTxts.confirm")
        }}</el-button>
        <el-button @click="resetForm('rulesForm')">{{
          $t("publicTxts.cancel")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getProductGroupList,
  getProductGroupById,
  setGroup,
  addGroup,
} from "@/api/productGroup";
export default {
  name: "ProductGroup",
  data() {
    return {
      titleForm: this.$t("productGroupCreate.title0"),
      dialogFormVisible: false,
      form: {
        sequenceValue: "",
      },
      rulesForm: {
        groupName: [
          {
            required: true,
            message: "Group name is required",
            trigger: "blur",
          },
        ],
        sequenceValue: [
          {
            required: true,
            message: "Product sequence is required",
            trigger: "change",
          },
        ],
      },
      currentPage: 1,
      total: 0,
      formInline: {
        page: 1,
        limit: 10,
        name: "",
      },
      tableData: [],
    };
  },
  created() {
    this.init();
    this.cacheGroup();
  },
  methods: {
    changeSequenceValue(e) {
      this.form.sequenceValue = e;
      this.$forceUpdate();
    },
    // 更新产品组缓存
    async cacheGroup() {
      getProductGroupList({
        page: 1,
        limit: 999,
      }).then((response) => {
        this.storage.set("productGroupList", response.data);
      });
    },
    toPage(id) {
      this.$router.push({
        path: "editGroup",
        query: { id },
      });
    },
    createGroup() {
      this.titleForm = this.$t("productGroupCreate.title0");
      this.dialogFormVisible = true;
    },
    editGroup(row) {
      this.titleForm = this.$t("productGroupCreate.title1");
      getProductGroupById(row).then((response) => {
        this.form = response.data;
        this.form.sequenceValue = response.data.sequence.value + "";
        this.dialogFormVisible = true;
      });
    },
    resetForm(formName) {
      this.form = {
        sequenceValue: "",
      };
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    addProductGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.sequence = this.storage
            .get("sequence")
            .filter((item) => item.value === this.form.sequenceValue)[0];
          addGroup(this.form).then((response) => {
            this.$message({
              message: this.$t("publicTxts.success"),
              type: "success",
            });
            this.resetForm("rulesForm");
            this.dialogFormVisible = false;
            this.init();
            this.cacheGroup();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    disableGroup(row) {
      this.$confirm(
        row.status.value === "1"
          ? this.$t("goodsTable.disableTip1")
          : this.$t("goodsTable.disableTip2"),
        this.$t("rulesManagement.tip"),
        {
          confirmButtonText: this.$t("rulesManagement.confirm"),
          cancelButtonText: this.$t("rulesManagement.cancel"),
          type: "warning",
        }
      )
        .then(async () => {
          let status = {};
          if (row.status.value === "1") {
            status.label = "Disable";
            status.value = "0";
          } else {
            status.label = "Enable";
            status.value = "1";
          }
          setGroup({
            id: row.id,
            status,
          }).then((response) => {
            this.$message({
              message: this.$t("createProduct.editSuccess"),
              type: "success",
            });
            this.init();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("rulesManagement.canceled"),
          });
        });
    },
    handleSizeChange(val) {
      this.formInline.limit = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.formInline.page = val;
      this.init();
    },
    init() {
      getProductGroupList(this.formInline).then((response) => {
        console.log(response);
        this.tableData = response.data;
        this.total = response.count;
      });
    },
    onSubmit() {
      this.formInline.page = 1;
      this.formInline.limit = 10;
      this.init();
    },
  },
};
</script>

<style lang="scss" scoped>
.oldPrice span {
  color: #c0c4cc !important;
  text-decoration: line-through !important;
}
.createGroup {
  .el-input,
  .el-select {
    width: 80%;
  }
}
.addBtn {
  margin-bottom: 12px;
}
</style>
